package com.huanghuai.retail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanghuai.retail.common.R;
import com.huanghuai.retail.entity.DeviceType;
import com.huanghuai.retail.mapper.DeviceTypeMapper;
import com.huanghuai.retail.service.DeviceService;
import com.huanghuai.retail.service.DeviceTypeService;
import com.huanghuai.retail.service.FactoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class DeviceTypeServiceImpl extends ServiceImpl<DeviceTypeMapper, DeviceType> implements DeviceTypeService {
    @Resource
    private FactoryService factoryService;
    @Resource
    private DeviceService deviceService;
    @Override
    public R<String> saves(DeviceType deviceType) {
        if(deviceType == null){
            return R.error("参数不能为空");
        }
        log.info("添加的对象：" + deviceType.toString());

        if(deviceType.getRegistrationData()==null||
                deviceType.getManufactureData()==null||
                deviceType.getFactoryId() == null||
                deviceType.getDeviceTypeName() == null||
                deviceType.getRowX() == null||
                deviceType.getColumnY() == null){
            return R.error("有字段为空");
        }

        if(factoryService.getById(deviceType.getFactoryId())==null){
            return R.error("厂家不存在");
        }
        save(deviceType);
        return R.success("添加设备类型成功");
    }

    @Override
    public R<String> updates(DeviceType deviceType) {
        if (deviceType == null) {
            return R.error("参数不能为空");
        }
        log.info("修改的对象："+deviceType.toString());
        if (deviceType.getRegistrationData()==null||
                deviceType.getManufactureData()==null||
                deviceType.getId()==null||
                deviceType.getFactoryId() == null ||
                deviceType.getDeviceTypeName() == null ||
                deviceType.getRowX() == null ||
                deviceType.getColumnY() == null) {
            return R.error("有字段为空");
        }
        if(factoryService.getById(deviceType.getFactoryId())==null){
            return R.error("厂家不存在");
        }
        if(updateById(deviceType))
            return R.success("修改设备类型成功");
        else
            return R.error("修改设备类型失败");
    }

    @Override
    public R<String> deletes(Long id) {
        if (id == null ||id<=0) {
            return R.error("参数值不合理");
        }
        DeviceType deviceType = getById(id);
        if (deviceType == null) {
            return R.error("设备类型不存在");
        }
        if(deviceService.findByDeviceTypeId(id)!=null){
            return R.error("该类型下有设备");
        }
        removeById(id);
        return R.success("删除设备类型成功");
    }

    @Override
    public R<DeviceType> getByIds(Long id) {
        if(id == null||id<=0){
            return R.error("参数值不合理");
        }
        DeviceType deviceType = getById(id);
        if (deviceType == null) {
            return R.error("设备类型不存在");
        }
        deviceType.setFactoryName(factoryService.getById(deviceType.getFactoryId()).getName());
        return R.success(deviceType);
    }

    @Override
    public R<Page<DeviceType>> pages(int page, int pageSize, String name) {
        if (name == null) name="";
        Page<DeviceType> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<DeviceType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(DeviceType::getId);
        queryWrapper.like(DeviceType::getDeviceTypeName, name);
        IPage<DeviceType> deviceTypeIPage = page(pageInfo, queryWrapper);
        if (deviceTypeIPage == null) {
            return R.error("查询失败");
        }
        List<DeviceType> deviceTypeList = deviceTypeIPage.getRecords();
        if (deviceTypeList == null) {
            return R.error("查询失败");
        }
        for (DeviceType deviceType: deviceTypeList) {
            deviceType.setFactoryName(factoryService.getById(deviceType.getFactoryId()).getName());
        }
        pageInfo.setRecords(deviceTypeList);
        pageInfo.setTotal(deviceTypeIPage.getTotal());
        return R.success(pageInfo);
    }
}
